const _ = require('lodash');
require('deepdash')(_);

let output=[]

const data = {
  id: 1,
  children: [{ id: 2, children: [{ id: 3, children: [] }] }],
};

console.log('original data: ', data);

let options = {
  includeRoot: true,
  childrenPath: 'children'
}
_.eachDeep(data, (value, key, parent, context) => {
  if (value == 2) {
    output.push(context.path + ' -> break! ');
    return false;
  }
  output.push(context.path);
});

console.log('Return false to skip children of current value', output);

output = [];
let breakLoop = false;
_.eachDeep(data, (v, k, parent, context) => {
  if (breakLoop || v == 2) {
    output.push(context.path + ' -> break! ');
    breakLoop = true;
    return false;
  }
  console.log(`value: `, v)
  console.log(`key: `, k)
  console.log(`parent: `, parent)
  // console.log(`context: `, context)

  console.log(`path: `, context.path)
  console.log(`parent: `, context.parent)
  console.log(`childrenPath: `, context.childrenPath)
  console.log(`parents: `, context.parents)

  output.push(context.path);
}, options);

console.log(
  'There is no way to break the iteration. Return false from rest of callbacks to stop iteration as fast as possible.',
  output
);
